﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using SobFS.IDAL;
using SobFS.Common.Database;

namespace SobFS.MySQLDAL
{
    /// <summary>
    /// 数据访问类:SysFunctionMenu
    /// </summary>
    public partial class SysFunctionMenu : ISysFunctionMenu
    {
        public SysFunctionMenu()
        { }

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("Id", "SysFunctionMenu");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SysFunctionMenu");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SobFS.Model.SysFunctionMenu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SysFunctionMenu(");
            strSql.Append("ParentId,MenuType,Code,Name,URL,Position,Description,IsObsoleted,IsEnabled)");
            strSql.Append(" values (");
            strSql.Append("@ParentId,@MenuType,@Code,@Name,@URL,@Position,@Description,@IsObsoleted,@IsEnabled)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@MenuType", MySqlDbType.VarChar,50),
					new MySqlParameter("@Code", MySqlDbType.VarChar,50),
					new MySqlParameter("@Name", MySqlDbType.VarChar,50),
					new MySqlParameter("@URL", MySqlDbType.VarChar,1024),
					new MySqlParameter("@Position", MySqlDbType.Int32,11),
					new MySqlParameter("@Description", MySqlDbType.Text),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit)};
            parameters[0].Value = model.ParentId;
            parameters[1].Value = model.MenuType;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.URL;
            parameters[5].Value = model.Position;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.IsObsoleted;
            parameters[8].Value = model.IsEnabled;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SobFS.Model.SysFunctionMenu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SysFunctionMenu set ");
            strSql.Append("ParentId=@ParentId,");
            strSql.Append("MenuType=@MenuType,");
            strSql.Append("Code=@Code,");
            strSql.Append("Name=@Name,");
            strSql.Append("URL=@URL,");
            strSql.Append("Position=@Position,");
            strSql.Append("Description=@Description,");
            strSql.Append("IsObsoleted=@IsObsoleted,");
            strSql.Append("IsEnabled=@IsEnabled");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@MenuType", MySqlDbType.VarChar,50),
					new MySqlParameter("@Code", MySqlDbType.VarChar,50),
					new MySqlParameter("@Name", MySqlDbType.VarChar,50),
					new MySqlParameter("@URL", MySqlDbType.VarChar,1024),
					new MySqlParameter("@Position", MySqlDbType.Int32,11),
					new MySqlParameter("@Description", MySqlDbType.Text),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit),
					new MySqlParameter("@Id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.ParentId;
            parameters[1].Value = model.MenuType;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.URL;
            parameters[5].Value = model.Position;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.IsObsoleted;
            parameters[8].Value = model.IsEnabled;
            parameters[9].Value = model.Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysFunctionMenu ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysFunctionMenu ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SobFS.Model.SysFunctionMenu GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ParentId,MenuType,Code,Name,URL,Position,Description,IsObsoleted,IsEnabled from SysFunctionMenu ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            SobFS.Model.SysFunctionMenu model = new SobFS.Model.SysFunctionMenu();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"] != null && ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MenuType"] != null && ds.Tables[0].Rows[0]["MenuType"].ToString() != "")
                {
                    model.MenuType = ds.Tables[0].Rows[0]["MenuType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Code"] != null && ds.Tables[0].Rows[0]["Code"].ToString() != "")
                {
                    model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["URL"] != null && ds.Tables[0].Rows[0]["URL"].ToString() != "")
                {
                    model.URL = ds.Tables[0].Rows[0]["URL"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Position"] != null && ds.Tables[0].Rows[0]["Position"].ToString() != "")
                {
                    model.Position = int.Parse(ds.Tables[0].Rows[0]["Position"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Description"] != null && ds.Tables[0].Rows[0]["Description"].ToString() != "")
                {
                    model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsObsoleted"] != null && ds.Tables[0].Rows[0]["IsObsoleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsObsoleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsObsoleted"].ToString().ToLower() == "true"))
                    {
                        model.IsObsoleted = true;
                    }
                    else
                    {
                        model.IsObsoleted = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["IsEnabled"] != null && ds.Tables[0].Rows[0]["IsEnabled"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsEnabled"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsEnabled"].ToString().ToLower() == "true"))
                    {
                        model.IsEnabled = true;
                    }
                    else
                    {
                        model.IsEnabled = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ParentId,MenuType,Code,Name,URL,Position,Description,IsObsoleted,IsEnabled ");
            strSql.Append(" FROM SysFunctionMenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }
        #endregion  Method
	
    }
}

